package com.gitee.feizns.quickstart.web.Interceptors.bucket4j.handler;

import com.gitee.feizns.quickstart.web.Interceptors.bucket4j.RateLimitHandler;
import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
import lombok.RequiredArgsConstructor;

/**
 * qps限流
 * @author feizns
 * @since 2025/10/28
 */
@RequiredArgsConstructor
public class Qps implements RateLimitHandler {

    /**
     * 带宽
     */
    private final Bandwidth bandwidth;

    @Override
    public Bucket bucket(String key) {
        return Bucket.builder().addLimit(bandwidth).build();
    }

    /**
     * QPS
     * @param times 次
     * @return {@link Bandwidth }
     */
    public static Bandwidth qps(int times) {
        return Bandwidth.builder().capacity(times).refillIntervally(times, java.time.Duration.ofSeconds(1)).build();
    }

}
